package boot.spring.service.impl;

import java.util.*;

import boot.spring.entity.Permission;
import boot.spring.entity.PermissionsModule;
import boot.spring.mapper.PermissionMapper;
import boot.spring.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import boot.spring.entity.Module;
import boot.spring.mapper.ModuleMapper;
import boot.spring.service.ModuleService;

@Service
public class ModuleServiceImpl implements ModuleService {
	/*模块表的mapper持久层*/
	@Autowired
	private ModuleMapper moduleMapper;

	/*权限表的mapper持久层*/
	@Autowired
	private PermissionMapper permissionMapper;

	//创建返回值数据类型的对象
	Result r=new Result();

	@Override/*根据角色id查询模块*/
	public List<Module> getModuleByRid(String roleId) {
		return moduleMapper.getModuleByRid(roleId);
	}


	@Override/*获取所有的模块*/
	public List<Map<String,Object>> ModuleSerAll() {

		List<Module> all=moduleMapper.ModuleMapAll();
		/*获取到所有的权限*/
		List<Permission> listper=permissionMapper.ListPerMapAll();
		List<Map<String,Object>> fuList=new ArrayList<>();
		List<Map<String,Object>> ziList=new ArrayList<>();
		List<Map<String,Object>> quanxianList=new ArrayList<>();
		for(Module m:all){
			if(m.getParentid().equals("i")){
				Map<String,Object> fu=new HashMap<>();
				fu.put("id",m.getMid());
				fu.put("title",m.getMname());
				fu.put("parentid",m.getParentid());
				fu.put("url",m.getUrl());
				fu.put("weight",m.getWeight());
				for(Module m2:all){
					if(m2.getParentid().equals(m.getMid())){
						Map<String,Object> zi=new HashMap<>();
						zi.put("id",m2.getMid());
						zi.put("title",m2.getMname());
						zi.put("parentid",m2.getParentid());
						zi.put("url",m2.getUrl());
						zi.put("weight",m2.getWeight());
						/*循环判断当前的子模块下面的权限*//*
						for(Permission per : listper){
							if(m2.getMid().equals(per.getMname())){*//*如果当前模块的名称等于当前权限的模块名称*//*
								Map<String,Object> quanxian=new HashMap<>();
								quanxian.put("id",per.getPid());
								quanxian.put("title",per.getPname());
								quanxian.put("mname",per.getMname());
								quanxian.put("pidentifier",per.getPidentifier());
								quanxianList.add(quanxian);*//*把获取到的权限添加到list集合里*//*
							}

						}
						zi.put("quanxian",quanxianList);*/
						ziList.add(zi);/*把获取到的子节点添加到list集合里*/
						/*quanxianList=new ArrayList<>();*/

					}


				}
				fu.put("children",ziList);/*再把当前模块的子节点添加到fu Map里面*/
				fuList.add(fu);/*再把fu Map添加到fulist集合里面*/
				ziList=new ArrayList<>();
			}
		}
		return fuList;
	}

	@Override/*给角色分配模块*/
	public Result RoleModuleSeroperation(String parentIds, String rId) {
		List<Module> mo=moduleMapper.getModuleByRid(rId);/*根据角色id获取当前角色的模块*/
		List mid=new ArrayList();/*存放当前角色所拥有的模块的id*/
		for(Module m:mo){
			String bb=m.getMid();
			mid.add(bb);
			System.out.println("当前角色所拥有的模块的id"+bb);
		}
		List<String> lis = Arrays.asList(parentIds.split(","));
		List mid2=new ArrayList();/*存放前台传过来的模块id*/
		for (int i = 0; i < lis.size(); i++) {
			if(lis.get(i).equals("")){
				break;
			}else{
				String aa=lis.get(i);
				mid2.add(aa);
				System.out.println("存放前台传过来的模块id"+aa);
			}
		}

	if(mid.size()!=0 && mid2.size()!=0){
		String resultStr="";
		/*循环判断*/
		for(int i=0;i<mid.size();i++){/*取出数据库有而前台没有的值，调用删除角色模块mapper接口删除*/
			boolean isOk=true;
			String str= (String) mid.get(i);
			for(int ii=0;ii<mid2.size();ii++){
				String str2= (String) mid2.get(ii);
				if(str.equals(str2)){
					isOk=false;
					break;
				}
			}
			if(isOk){
				Map<String,Object> m=new HashMap<>();
				m.put("rid",rId);
				m.put("mid",str);
				Integer del=moduleMapper.RoleModuleMapDel(m);
				System.out.println("删除bbbb"+del);
			}
		}

		String resultStr2="";
		/*循环判断*/
		for(int i2=0;i2<mid2.size();i2++){/*取出前台传过来的而数据库没有的值，调用新增角色模块mapper接口新增*/
			boolean isOk=true;
			String str= (String) mid2.get(i2);
			for(int ii=0;ii<mid.size();ii++){
				String str2= (String) mid.get(ii);
				if(str.equals(str2)){
					isOk=false;
					break;
				}
			}
			if(isOk){
				Map<String,Object> m=new HashMap<>();
				m.put("rid",rId);
				m.put("mid",str);
				Integer add=moduleMapper.RoleModuleMapAdd(m);
				System.out.println("aaaaaaaa"+add);
			}
		}
	}else if(mid.size() == 0){/*如果后台没有获取到数据的话则直接添加数据*/
		for(int t=0;t<mid2.size();t++){
			String str= (String) mid2.get(t);
			Map<String,Object> m=new HashMap<>();
			m.put("rid",rId);
			m.put("mid",str);
			Integer add=moduleMapper.RoleModuleMapAdd(m);
		}

	}else if (mid2.size() == 0 ){/*如果前台没有获取到数据的话则直接删除数据*/
		for(int s=0;s<mid.size();s++){
			String str= (String) mid.get(s);
			Map<String,Object> m=new HashMap<>();
			m.put("rid",rId);
			m.put("mid",str);
			Integer add=moduleMapper.RoleModuleMapDel(m);
		}
	}

		r=new Result(0,"请求成功");

		return r;
	}

	@Override	/*修改模块表中的数据*/
	public Result ModuleSerUp(Module up) {

		/*判断当前修改的模块是否在同级下重名*/
		Integer all=moduleMapper.FuidModuleAll(up);
		if(all>0){
			r=new Result(1,"模块名重复");
		}else{
			Integer upSer=moduleMapper.ModuleMapUp(up);
			if(upSer>0){
				r=new Result(0,"修改成功");
			}else{
				r=new Result(1,"修改失败");
			}
		}

		return r;
	}

	@Override/*删除模块表中的数据*/
	public Result ModuleSerDel(Module del) {
		Integer delSer=moduleMapper.ModuleMapDel(del);
		if(delSer>0){
			r=new Result(0,"删除成功");
		}else{
			r=new Result(1,"删除失败");
		}
		return r;
	}

	@Override/*新增模块表中的数据*/
	public Result ModuleSerAdd(Module add) {

		/*判断当前修改的模块是否在同级下重名*/
		Integer all=moduleMapper.FuidModuleAll(add);
		if(all>0){
			r=new Result(1,"模块名重复");
		}else{
			Integer addSer=moduleMapper.ModuleMapAdd(add);
			if(addSer>0){
				r=new Result(0,"新增成功");
			}else{
				r=new Result(1,"新增失败");
			}
		}
		return r;
	}




}
